﻿using System.Runtime.Versioning;
using System.Windows.Forms;
using Microsoft.Win32;

namespace zykControl
{
    /// <summary>
    /// zykControl全局功能
    /// </summary>
    [SupportedOSPlatform("windows")]
    public static class MdlControl
    {
        /// <summary>
        /// 从注册表载入DataGridView的列宽设置并应用
        /// </summary>
        /// <param name="theDataGridView"></param>
        /// <param name="theKeyMain"></param>
        /// <param name="theModule"></param>
        public static void LoadListLayout(DataGridView theDataGridView, string theKeyMain, string theModule = "")
        {
            if (string.IsNullOrEmpty(theKeyMain)) return;
            if (string.IsNullOrEmpty(theDataGridView.FindForm()?.Name)) return;
            if (string.IsNullOrEmpty(theDataGridView.Name)) return;

            var dKey = Registry.CurrentUser.CreateSubKey("Software").CreateSubKey(theKeyMain).CreateSubKey(theDataGridView.FindForm()!.Name + (string.IsNullOrEmpty(theModule) ? "" : "_" + theModule)).CreateSubKey(theDataGridView.Name);
            for (int xx = 0, loopTo = theDataGridView.Columns.Count - 1; xx <= loopTo; xx++)
            {
                var aa = dKey.GetValue(xx.ToString())?.ToString();
                if (string.IsNullOrEmpty(aa)) continue;
                if (int.TryParse(aa, out var intValue) == false) continue;

                theDataGridView.Columns[xx].Width = intValue;
            }

            dKey.Close();
        }

        /// <summary>
        /// 把DataGridView的列宽设置保存到注册表
        /// </summary>
        /// <param name="theDataGridView"></param>
        /// <param name="theKeyMain"></param>
        /// <param name="theModule"></param>
        public static void SaveListLayout(DataGridView theDataGridView, string theKeyMain, string theModule = "")
        {
            if (string.IsNullOrEmpty(theKeyMain)) return;
            if (string.IsNullOrEmpty(theDataGridView.FindForm()?.Name)) return;
            if (string.IsNullOrEmpty(theDataGridView.Name)) return;

            var dKey = Registry.CurrentUser.CreateSubKey("Software").CreateSubKey(theKeyMain).CreateSubKey(theDataGridView.FindForm()!.Name + (string.IsNullOrEmpty(theModule) ? "" : "_" + theModule)).CreateSubKey(theDataGridView.Name);
            for (int xx = 0, loopTo = theDataGridView.Columns.Count - 1; xx <= loopTo; xx++)
                dKey.SetValue(xx.ToString(), theDataGridView.Columns[xx].Width.ToString());
            dKey.Close();
        }

        /// <summary>
        /// 从注册表载入ListView的列宽设置并应用
        /// </summary>
        /// <param name="theListView"></param>
        /// <param name="theKeyMain"></param>
        public static void LoadListLayout(ListView theListView, string theKeyMain)
        {
            if (string.IsNullOrEmpty(theKeyMain)) return;
            if (string.IsNullOrEmpty(theListView.FindForm()?.Name)) return;
            if (string.IsNullOrEmpty(theListView.Name)) return;

            var dKey = Registry.CurrentUser.CreateSubKey("Software").CreateSubKey(theKeyMain).CreateSubKey(theListView.FindForm()!.Name).CreateSubKey(theListView.Name);
            for (int xx = 0, loopTo = theListView.Columns.Count - 1; xx <= loopTo; xx++)
            {
                string aa = dKey.GetValue(xx.ToString())?.ToString() ?? "";
                if (string.IsNullOrEmpty(aa)) continue;
                if (int.TryParse(aa, out var intValue) == false) continue;
                theListView.Columns[xx].Width = intValue;
            }

            dKey.Close();
        }

        /// <summary>
        /// 把DataGridView的列宽设置保存到注册表
        /// </summary>
        /// <param name="theListView"></param>
        /// <param name="theKeyMain"></param>
        public static void SaveListLayout(ListView theListView, string theKeyMain)
        {
            if (string.IsNullOrEmpty(theKeyMain)) return;
            if (string.IsNullOrEmpty(theListView.FindForm()?.Name)) return;
            if (string.IsNullOrEmpty(theListView.Name)) return;

            var dKey = Registry.CurrentUser.CreateSubKey("Software").CreateSubKey(theKeyMain).CreateSubKey(theListView.FindForm()!.Name).CreateSubKey(theListView.Name);
            for (int xx = 0, loopTo = theListView.Columns.Count - 1; xx <= loopTo; xx++)
                dKey.SetValue(xx.ToString(), theListView.Columns[xx].Width.ToString());
            dKey.Close();
        }

        /// <summary>
        /// 设置DataGridView为双缓冲模式
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="setting"></param>
        public static void SetDoubleBuffered(this DataGridView dgv, bool setting)
        {
            var dgvType = dgv.GetType();
            var pi = dgvType.GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            pi?.SetValue(dgv, setting, null);
        }

        /// <summary>
        /// 设置DataGridView为双缓冲模式
        /// </summary>
        /// <param name="dgv"></param>
        /// <param name="setting"></param>
        public static void SetDoubleBuffered(this ZykDataGrid dgv, bool setting)
        {
            var dgvType = dgv.GetType();
            var pi = dgvType.GetProperty("DoubleBuffered", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            pi?.SetValue(dgv, setting, null);
        }
    }
}